import joystick from "./joystick";
const {ccclass, property} = cc._decorator;

@ccclass
export default class ball_ctrl extends cc.Component {

    @property(joystick)
    joystick: joystick = null;

    @property(cc.Node)
    camera: cc.Node = null

    @property
    speed: number = 200;

    private body: cc.RigidBody = null

    onLoad () {
        this.body = this.getComponent(cc.RigidBody)
    }
    start(): void {
        if (this.camera) {
            this.camera.setPosition(this.node.getPosition())
        }
    }

    update (dt) {
        if (this.camera) {
            this.camera.setPosition(this.node.getPosition())
        }
        if (this.joystick.dir.x === 0 && this.joystick.dir.y === 0) {
            this.body.linearVelocity = cc.v2(0, 0)
            return;
        }

        const vx:number = this.speed * this.joystick.dir.x;
        const vy:number = this.speed * this.joystick.dir.y;
        this.body.linearVelocity = cc.v2(vx, vy)

        const r: number = Math.atan2(this.joystick.dir.y, this.joystick.dir.x)
        let degree: number = r * 180 / Math.PI;

        // rotation 顺时针
       /*  degree = 360 - degree
        degree = degree + 90
        this.node.rotation = degree */

        // angle 逆时针
        this.node.angle = degree - 90
    }
}
